$('.datatable-basic').DataTable({
	 "ajax" : {
		"url" : contextpath + "hotRecommend/list",
		"data" : function(d) {
            var query = $("#query").serializeObject();
            query["size"] = d.length;
            query["page"] = (d.start/d.length);
			return query;
		},
		"dataFilter": function(data){
            var json = jQuery.parseJSON( data );
            json.recordsTotal = json.totalElements;
            json.recordsFiltered = json.totalElements;
            json.data = json.content;
 
            return JSON.stringify( json );
        }
	 },
	 "columns": [
         { "data": "name" },
         {
        	 render : function(data, type, row, meta) {
        		 return '<img width=50 hight=30 src='+row.image+'>';
             }
         },
         { "data": "url" },
         { "data": "createTime" },
         {
            render : function(data,type, row, meta) {
                return $("#btnGroup").html();
            }
        }
     ],
     "drawCallback": function( settings ) {
    	 $(".btn-add").click(function(){
    		 $("#imgUploader").find("img").remove();
    	 });
		 $(".btn-edit").click(function(){
			$("#edit").validate().resetForm();
		    $("#edit").fill($('.datatable-basic').DataTable().row($(this).closest("tr")).data());
		    $(".select").trigger("change");
		    
		    var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
		    $(":input[name='id']").val(row.id);
		    $("#imgUploader").find("img").remove();
		    //判断图片又图片则追加显示
		    if(row.image){
		    	$("#imgUploader").append('<img width=200 hight=100 src=' + row.image+'>');
		    }
		 });
		 
		 $(".btn-del").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			swal({
				title : "修改状态",
				text : "确定要删除热门推荐" + row.name + "?",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "确定",
				cancelButtonText: "取消",
				closeOnConfirm : false
			},function() {
				$.get(contextpath + "hotRecommend/delete/" + row.id, function(d){
					$('.datatable-basic').DataTable().draw();
					swal("删除热门推荐" +  row.name  + "成功","", "success");
				})
			});
			
		 });
      }
});

$(function(){
	displayName = $("#displayName").val();
	
	$(".btn-add").click(function(){
		$("#edit").validate().resetForm();
		
		$("#edit")[0].reset();
	});
	
	$("#submitEdit").click(function(){
		if($("#edit").valid()){
			var url = $("#url").val();
			url=url.substr(0,7).toLowerCase()=="http://"?url:"http://"+url;
			$("#url").val(url);
			$.post(contextpath + "hotRecommend/save", $("#edit").serialize(), function(){
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
				swal("编辑热门推荐成功","", "success");
			})
		}
	});
	 
	$("#submit").click(function(){
		$('.datatable-basic').DataTable().draw();
	});

	$('select').select2({
	    minimumResultsForSearch: Infinity
	});
	$("#uploadfile").fileinput({
        language : 'zh',
        uploadUrl : contextpath + 'hotRecommend/upload',
        uploadExtraData:function (previewId, index) {    
        	var data = {
        		
        	};
        	return data;
        },
        showUpload : true, //是否显示上传按钮
        showRemove : true //显示移除按钮
    });
	
	$("#uploadfile").on("fileuploaded", function (event, data, previewId, index) {
		$('.fileinput-upload-button').hide();
		var ref = $(this).attr("data-ref");
        $("input[name='" + ref + "']").val(data.response.data);
        $("#imgUploader").find("img").attr("src", data.response.data);
    }); 
})


